package com.block.sms.service.send;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.db.entity.SmsConfigInfo;
import com.block.db.entity.SmsTemplate;
import com.block.sms.dto.SmsBalanceDTO;
import com.block.sms.dto.SmsResultDTO;
import com.block.sms.service.ISmsSendService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ZhuTongSmsSendServiceImpl implements ISmsSendService {

    private static final Logger log = LogFactory.getCommLog();

    @Override
    public SmsResultDTO sendSms(String mobile, String outId, SmsTemplate smsTemplate, SmsConfigInfo smsConfigInfo, JSONObject paramObject) {

        SmsResultDTO resultDTO = new SmsResultDTO();
        try {
            JSONArray paramArr = paramObject.getJSONArray("paramArr");

            HttpRequest httpRequest = HttpUtil.createPost(smsConfigInfo.getUrl());

            String username = smsConfigInfo.getAccount();
            //格式话短信内容
//            String content = StrUtil.format(smsTemplate.getTemplateContext(), paramArr.stream().toArray());

            long tkey = System.currentTimeMillis() / 1000;

            String secret = smsConfigInfo.getSecret();

            String Md5Password = SecureUtil.md5(secret).toLowerCase(Locale.ROOT);

            String password = SecureUtil.md5(Md5Password + tkey).toLowerCase(Locale.ROOT);

            String ext = RandomUtil.randomNumbers(8);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("username", username);
            paramMap.put("password", password);
            paramMap.put("tKey",tkey );
            paramMap.put("signature", "【昇再】");
            paramMap.put("tpId",smsTemplate.getTemplateId());
            paramMap.put("ext", ext);
            paramMap.put("time","");
            paramMap.put("extend","");

            List<JSONObject> records = new ArrayList<>();

            String verifyCode = paramArr.getString(0);

            JSONObject tpContent = new JSONObject();
            tpContent.put("code", verifyCode);

            JSONObject record1 = new JSONObject();
            record1.put("mobile",mobile);
            record1.put("tpContent",tpContent);

            records.add(record1);

            paramMap.put("records",records);

            httpRequest.body(JSON.toJSONString(paramMap));

            String bodyResult = httpRequest.execute().body();
            log.info("【助通短信服务】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
                    smsConfigInfo.getUrl(), JSON.toJSONString(paramMap), bodyResult);

            JSONObject resultJson = JSON.parseObject(bodyResult);
            Integer code = resultJson.getInteger("code");
            String msg = resultJson.getString("msg");

            resultDTO.setData(bodyResult);
            if (StrUtil.equals("200", String.valueOf(code))) {
                resultDTO.setCode(AppRspCodeConstant.SUCCESS);
                resultDTO.setHttpStatus(String.valueOf(code));
                resultDTO.setRequestId(outId);
                resultDTO.setMessage(msg);
                return resultDTO;
            }
            resultDTO.setMessage(msg);
        } catch (Exception exception) {
            log.error(String.format("【助通短信服务】发送发生异常【mobile】%s【code】%s", mobile, paramObject.toJSONString()), exception);
        }
        resultDTO.setCode(AppRspCodeConstant.OPERATION_FAILURE);
        return resultDTO;
    }

    @Override
    public SmsBalanceDTO queryBalanceInfo(SmsConfigInfo smsConfigInfo) {
        return null;
    }

}
